﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ContactCRUD.Classes
{
    public class DelegateCommand<T> : System.Windows.Input.ICommand
    {
        #region Data

        private readonly Action<T> executeMethod = null;
        private readonly Predicate<T> canExecuteMethod = null;
         
        #endregion

        /// <summary>
        ///     Constructor
        /// </summary>
        public DelegateCommand(Action<T> executeMethod)
            : this(executeMethod, null)
        {

        }

        public DelegateCommand(Action<T> execute, Predicate<T> canExecute)
        {
            executeMethod = execute;
            canExecuteMethod = canExecute;
        }

        public void RiseCanExecuteEvent()
        {
            
        }

        public bool CanExecute(object parameter)
        {
            if (canExecuteMethod == null)
            {
                return false;
            }
            else
            {
                return canExecuteMethod((T)parameter);
            }
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                System.Windows.Input.CommandManager.RequerySuggested += value;
            }
            remove
            {
                System.Windows.Input.CommandManager.RequerySuggested += value;
            }
        }

        public void Execute(object parameter)
        {
            executeMethod((T)parameter);
        }
    }
    public class DelegateCommand: System.Windows.Input.ICommand
    {
        internal readonly Predicate<object> canExecute = null;
        internal readonly Action<object> execute = null;

        public DelegateCommand()
        {
            // nimic
        }
        public DelegateCommand(Action<object> execute): this(execute, null)
        {
            this.execute = execute;
        }

        public DelegateCommand(Action<object> execute,Predicate<object> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }
        bool System.Windows.Input.ICommand.CanExecute(object parameter)
        {
            if (canExecute == null)
            {
                return true;
            }

            return canExecute(parameter);
        }

        event EventHandler System.Windows.Input.ICommand.CanExecuteChanged
        {
            add { System.Windows.Input.CommandManager.RequerySuggested += value; }
            remove { System.Windows.Input.CommandManager.RequerySuggested -= value; }
        }

        void System.Windows.Input.ICommand.Execute(object parameter)
        {
            execute(parameter);
        }
    }
}
